<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/10/25
  Time: 8:35
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%!
    public List<Map<String,String>> getTable(String a,String b){
        List<Map<String,String>> list = new ArrayList<>();
        String driverName = "com.mysql.cj.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/mall?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/shanghai";
        String userName = "root";
        String pwd = "12345678";
        try {
//          设置数据库驱动  加载数据库驱动
            Class.forName(driverName);

//            连接数据库
            Connection con= DriverManager.getConnection(url,userName,pwd);

//            创建sql语句
            String sql = "select * from t_student where 1=1";
            if(a!=null && a!=""){
                sql+=" and stu_name="+a;
            }
            if(b!=null && b!=""){
                sql+=" and sex="+b;
            }

//            创建执行sql的对象，对象名Statement
            Statement stament= con.createStatement();

//            执行sql并返回sql执行结果
            ResultSet rs= stament.executeQuery(sql);
            while (rs.next()){
                Map<String,String> map = new HashMap<>();
                String stuName=rs.getString("stu_name");
                map.put("stuName",stuName);
                String sex = rs.getString("sex");
                map.put("sex",sex);

                list.add(map);
            }
//            关闭连接且释放资源
            rs.close();
            con.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

%>
<form action="mysql2.jsp">
    <table>
        <tr>
            <th>姓名：<input name="name" ></th>
            <th>
                性别：
                <select name="sex">
                    <option value="">请选择</option>
                    <option value="男">男</option>
                    <option value="女">女</option>
                </select>
            </th>
            <th>
                <input type="submit"  value="提交">
            </th>
        </tr>

    </table>




</form>





</body>
</html>
